<?php

App::import('Controller', 'Products');

class TestProductsController extends ProductsController
{
    var $name = 'Products';
    var $autoRender = false;

    function redirect($url, $status = null, $exit = true) {
        $this->redirectUrl = $url;
    }

    function render($action = null, $layout = null, $file = null) {
        $this->renderedAction = $action;
    }

    function _stop($status = 0) {
        $this->stopped = $status;
    }
}

class ProductControllerTestCase extends CakeTestCase
{
    var $fixtures = array('app.product', 'app.user');

    function startCase() {
        echo '<h2>Product Controller</h2>';
    }
    function endCase() {
    }
    function startTest($method) {
        echo '<h3>Starting method ' . $method . '</h3>';
        $this->Products = new TestProductsController();
        $this->Products->constructClasses();
        $this->Products->Component->initialize($this->Products);
    }
    function endTest($method) {
        echo '<hr />';
        unset($this->Products);
        ClassRegistry::flush();
    }

    function testView() {
        $vars = $this->testAction('/products/view/1', 'vars');

        // Check if all data is loaded correctly
        $product = $vars['Product']['Product'];
        $this->assertEqual($product['id'], 1);
        $this->assertEqual($product['user_id'], 5);
        $this->assertEqual($product['manufacturer_id'], 3);
        $this->assertEqual($product['category_id'], 5);
        $this->assertEqual($product['name'], 'Amazon Kindle Paperwhite');

        $view = $this->testAction('/products/view/1', 'view');
        // Check if javascript is loaded properly
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/product.js"\>/', $view);
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/cart.js"\>/', $view);
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/product_cart.js"\>/', $view);
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/facebook_functions.js"\>/', $view);
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/jquery.rateit.min.js"\>/', $view);

        // Check if css files are loaded
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/product.css" \/>/', $view);
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/rateit.css" \/>/', $view);
    }

    function testAddNoPermission() {
        // Test with normal user account
        $this->Products->Session->write('Auth.User', array(
            'id' => '12',
            'username' => 'ngthanhtrung23@gmail.com',
            'role' => '1'
        ));
        $this->Products->params = Router::parse('/products/add');
        $this->Products->beforeFilter();
        $this->Products->Component->startup($this->Products);
        $this->Products->add();

        // Check if user is redirected
        $this->assertEqual($this->Products->redirectUrl, array('controller' => 'users', 'action' => 'login'));
    }

    function testAddHasPermission() {
        // Login using admin account

        $this->Products->Session->write('Auth.User', array(
            'id' => '5',
            'username' => 'invincible_kid23@yahoo.com',
            'role' => '0'
        ));

        // Check if variables are loaded correctly
        $vars = $this->testAction('/products/add', 'vars');
        $this->assertEqual($vars['categories']['1'], 'Laptop');
        $this->assertEqual($vars['categories']['3'], 'Smartphone');
        $this->assertEqual($vars['categories']['4'], 'Gadgets');
        $this->assertEqual($vars['categories']['5'], 'Others');

        $this->assertEqual($vars['manufacturers']['1'], 'Apple Inc.');
        $this->assertEqual($vars['manufacturers']['2'], 'IBM');
        $this->assertEqual($vars['manufacturers']['3'], 'Amazon');
        $this->assertEqual($vars['manufacturers']['4'], 'Samsung');
        $this->assertEqual($vars['manufacturers']['5'], 'Canon');
        $this->assertEqual($vars['manufacturers']['6'], 'Sony');

        $this->assertEqual($vars['user']['User']['id'], 5);

        // Testing contents
        $view = $this->testAction('/products/add', 'view');

        // Check if javascript files are loaded
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/ckeditor\/ckeditor.js"\>/', $view);
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/modify_image.js"\>/', $view);

        // Check if css files are loaded
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/form.css" \/>/', $view);
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/product.css" \/>/', $view);
    }

    function testEditNoPermission() {
        // Test with normal user account
        $this->Products->Session->write('Auth.User', array(
            'id' => '12',
            'username' => 'ngthanhtrung23@gmail.com',
            'role' => '1'
        ));
        $this->Products->params = Router::parse('/products/edit/1');
        $this->Products->beforeFilter();
        $this->Products->Component->startup($this->Products);
        $this->Products->add();

        // Check if user is redirected
        $this->assertEqual($this->Products->redirectUrl, array('controller' => 'users', 'action' => 'login'));
    }

    function testEditHasPermission() {
        // Login using admin account

        $this->Products->Session->write('Auth.User', array(
            'id' => '5',
            'username' => 'invincible_kid23@yahoo.com',
            'role' => '0'
        ));

        // Check if variables are loaded correctly
        $vars = $this->testAction('/products/edit/1', 'vars');
        $this->assertEqual($vars['categories']['1'], 'Laptop');
        $this->assertEqual($vars['categories']['3'], 'Smartphone');
        $this->assertEqual($vars['categories']['4'], 'Gadgets');
        $this->assertEqual($vars['categories']['5'], 'Others');

        $this->assertEqual($vars['manufacturers']['1'], 'Apple Inc.');
        $this->assertEqual($vars['manufacturers']['2'], 'IBM');
        $this->assertEqual($vars['manufacturers']['3'], 'Amazon');
        $this->assertEqual($vars['manufacturers']['4'], 'Samsung');
        $this->assertEqual($vars['manufacturers']['5'], 'Canon');
        $this->assertEqual($vars['manufacturers']['6'], 'Sony');

        $this->assertEqual($vars['user']['User']['id'], 5);

        // Testing contents
        $view = $this->testAction('/products/edit/1', 'view');

        // Check if javascript files are loaded
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/ckeditor\/ckeditor.js"\>/', $view);
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/modify_image.js"\>/', $view);

        // Check if css files are loaded
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/form.css" \/>/', $view);
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/product.css" \/>/', $view);
    }

    function testDeleteNoPermission() {
        // Test with normal user account
        $this->Products->Session->write('Auth.User', array(
            'id' => '12',
            'username' => 'ngthanhtrung23@gmail.com',
            'role' => '1'
        ));
        $this->Products->params = Router::parse('/products/delete');
        $this->Products->beforeFilter();
        $this->Products->Component->startup($this->Products);
        $this->Products->add();

        // Check if user is redirected
        $this->assertEqual($this->Products->redirectUrl, array('controller' => 'users', 'action' => 'login'));
    }

    function testDeleteHasPermission() {
        // Login using admin account

        $this->Products->Session->write('Auth.User', array(
            'id' => '5',
            'username' => 'invincible_kid23@yahoo.com',
            'role' => '0'
        ));
        $this->Products->params = Router::parse('/products/delete');
        $this->Products->beforeFilter();
        $this->Products->Component->startup($this->Products);
        $this->Products->add();

        $this->assertTrue(!isset($this->Products->redirectUrl));
    }
}
?>
